package com.tchhy.provider.net.interceptor

import android.text.TextUtils
import com.tchhy.provider.URLConstant
import okhttp3.Interceptor
import okhttp3.Response

/**
 * 通过方法拦截器和retrofit的header注解动态替换BaseUrl
 *
 */
class DynamicBaseUrlInterceptor : Interceptor {

    override fun intercept(chain: Interceptor.Chain): Response {
        var request = chain.request()
        val header = request.header(URLConstant.OKHEADER_DYNAMICBASEURL)
        if (!TextUtils.isEmpty(header)) {
            request = request.url
                    .newBuilder()
                    .host(header!!)
                    .build().run { request.newBuilder().url(this).build() }
        }
        return chain.proceed(request)
    }
}